package com.howard.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import com.howard.exception.BeanPropertyExcpetion;

/**
 * User
 *
 * @author Howard.He
 * @version 1.0
 */
@SuppressWarnings("serial")
public class User implements Serializable {

	private Long id;
	private String name;
	private String accountName;
	private String mail; // 唯一
	private String password;
	private String mobile;
	private String phone;
	private String sex;
	private Integer status; // 0 激活，1锁定
	private Date createDate;
	private Date modifyDate;
	private String remark;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAccountName() {
		return accountName;
	}

	public void setAccountName(String accountName) {
		this.accountName = accountName;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Date getModifyDate() {
		return modifyDate;
	}

	public void setModifyDate(Date modifyDate) {
		this.modifyDate = modifyDate;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public void validate() {
		if (StringUtils.isBlank(name)) {
			throw new BeanPropertyExcpetion(
					"property [name] must be not null or emptry");
		}
		if (StringUtils.isBlank(accountName)) {
			throw new BeanPropertyExcpetion(
					"property [accountName] must be not null or emptry");
		}
		if (StringUtils.isBlank(mail)) {
			throw new BeanPropertyExcpetion(
					"property [mail] must be not null or emptry");
		}
		Pattern mailPattern = Pattern
				.compile("\\w+@(\\w+\\.)+[a-z]{2,3}");
		if (!mailPattern.matcher(mail).matches()) {
			throw new BeanPropertyExcpetion("property [mail] match incorrent");
		}
		if (StringUtils.isBlank(password)) {
			throw new BeanPropertyExcpetion(
					"property [password] must be not null or emptry");
		}
		if (StringUtils.isBlank(mobile)) {
			throw new BeanPropertyExcpetion(
					"property [mobile] must be not null or emptry");
		}
		if (StringUtils.isBlank(phone)) {
			throw new BeanPropertyExcpetion(
					"property [phone] must be not null or emptry");
		}
		if (StringUtils.isBlank(sex)) {
			throw new BeanPropertyExcpetion(
					"property [sex] must be not null or emptry");
		}
		if (!(StringUtils.equals(sex, "M") || StringUtils.equals(sex, "F"))) {
			throw new BeanPropertyExcpetion("property [sex] must be 'M' or 'F'");
		}
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.MULTI_LINE_STYLE);
	}
}
